<?php
include ('common/constants.php');
include ('common/db.php');
session_start();

if ((!isset($_SESSION['user_email'])) || ($_SESSION['user_email']=='')) {
  header('location: login.php');
}

function ifMale($email){
  $sql = "SELECT gender FROM `user` where email='" . $email . "';";
  if($result = mysql_query($sql)){
    if(mysql_result($result, 0) =="F"){
      return "fe";
    }
  }
}

function getNoOfSeats($advert_id){
  $sql2 = " SELECT count(seat_number) FROM `seat` WHERE seat_status = 'open' AND advert_id='". $advert_id . "';";
  //echo $sql2;
  if($result = mysql_query($sql2)){
    return mysql_result($result, 0);
  }
  //echo mysql_error();
}

function showVehicleType($advert_id){
  $sql2 = " SELECT `vehicle_type_id` FROM `advertisement` WHERE advert_id='". $advert_id . "';";
  //echo $sql2;
  if($result = mysql_query($sql2)){
      if (mysql_result($result, 0)=="1"){
        return "car";
      }elseif (mysql_result($result, 0)=="2"){
        return "van";
      }elseif (mysql_result($result, 0)=="3"){
        return "dcab";
      }elseif (mysql_result($result, 0)=="4"){
        return "bus";
      }
  }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Carpooling - My Posts</title>
  <link href="resources/css/bootstrap.min.css" rel="stylesheet">
  <link href="resources/css/bootstrap-responsive.min.css" rel="stylesheet">
  <link href="resources/css/datepicker.css" rel="stylesheet">
  <link href="resources/css/jquery.timepicker.css" rel="stylesheet">

  <style type="text/css">
  .cus_row {
  	-moz-box-sizing: border-box;
  	float:left;
  	min-height: 1px;
  	padding-left: 15px;
  	padding-right: 15px;
  	position: relative;
  }
  </style>

</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container"> 
    	<a class="btn btn-navbar" style="margin-top: 25px;" data-toggle="collapse" data-target=".nav-collapse">
          <span class="icon-bar"></span> 
          <span class="icon-bar"></span> 
          <span class="icon-bar"></span> 
      	</a> <a class="brand" style="text-align:center;" href="home.php"><img src="resources/icons/share_ride.png" style="max-width:55px; padding-right:15px"/>Carpooling.lk</a>
        <div class="nav-collapse collapse">
            <ul class="nav navbar-nav">
              <li style="text-align:center; float:left">
                <a href="advertise.php"><img src="resources/icons/speaker.png" style="max-width:40px; min-width: 20px;"/><br />Advertise</a>
              </li>
              <li style="text-align:center; float:left">
                <a href="publish.php"><img src="resources/icons/publish.png" style="max-width:40px; min-width: 20px;"/><br />Publish</a>
              </li>
              <li style="text-align:center; float:left">
                <a href="#notifPanel" data-toggle="modal"><img src="resources/icons/notification.png" style="max-width:40px; min-width: 20px;"/><br />Notifications</a>
              </li>
              <li class="active" style="text-align:center; float:left">
                <a href="my_posts.php"><img src="resources/icons/my_posts.png" style="max-width:40px; min-width: 20px;"/><br />My Posts</a>
              </li>
              <li style="text-align:center; float:left">
                <a href="user_details.php"><img src="resources/icons/user_details.png" style="max-width:40px; min-width: 20px;"/><br />User Details</a>
              </li>
              <li style="text-align:center; float:left">
                <a href="preferences.php"><img src="resources/icons/preferences.png" style="max-width:40px; min-width: 20px;"/><br />Preferences</a>
              </li>
              <li style="text-align:center; float:left">
                <a data-toggle="collapse" data-target="#srch_panel"><img src="resources/icons/search.png" style="max-width:40px; min-width: 20px;"/><br />Search</a>
              </li>
              <li style="text-align:center; float:left; line-height: 0px; max-width: 200px;">
                &nbsp;&nbsp;<a class="" data-toggle="collapse"><h4>Hi <?php echo $_SESSION['user_name']?></h4></a><br />
              </li>
              <li style="text-align:center; float:left">
                &nbsp;&nbsp;<a style="padding:20px; margin: 0;" href="login.php">Sign Out</a>
              </li>
            </ul>
        </div>
        <!-- Search Panel -->
        <div id="includedSrchPanel" class="cus_row" style="width:100%"></div>
    </div>
  </div>
</div>
<div style="height:85px"></div>
<div class="container">
  <?php 
  $sql = " SELECT a.*, u.`name` as username, u.email FROM advertisement a, user u ";
  $sql .= " WHERE a.email='". $_SESSION["user_email"]. "' AND a.email = u.email ORDER BY a.status, a.travel_date DESC";

  //echo $sql;
  $result = mysql_query($sql) or trigger_error(mysql_error());
  $num = mysql_num_rows($result);
  $i = 0;
  for ( $i=0; $i < $num; $i++ )
  {
	  
$travelDateFormatted = DateTime::createFromFormat('Y-m-d', mysql_result($result, $i, "a.travel_date"));
  ?>
   <div class="row">
    <div class="btn btn-default accept-btn" style="width:100%" id="<?php echo mysql_result($result,$i,"advert_id"); ?>" advType="<?php echo mysql_result($result,$i,"advertisement_type"); ?>" >
      <div style="padding-top: 20px; height: 173px;">
        <div class="cus_row" style="width:10%; text-align: left;"> 
          <img class="img-responsive" src="resources/icons/<?php echo ifMale(mysql_result($result,$i,"email")); ?>male_user_icon.png" style="min-width: 40px;"/> 
          <div><?php echo mysql_result($result,$i,"username"); ?></div>
        </div>
        <div class="cus_row" style="width:68%;">
          <h3 style="font-style:italic; color:#36C"><?php echo nl2br(mysql_result($result,$i,"origin")); ?> <img class="img-responsive" src="resources/icons/to.png"  style="min-width: 40px;"/> <?php echo nl2br(mysql_result($result,$i,"destination")); ?></h3>
          <h4 style="color:#F30"><span id="<?php echo mysql_result($result,$i,"a.advert_id"); ?>"><?php echo getNoOfSeats(mysql_result($result,$i,"a.advert_id")); ?></span> seat(s) <?php if (mysql_result($result,$i,"advertisement_type")=="advert") {
            echo "available";
          } else {
            echo "required";
          }
           ?> </h4>
           <?php if (mysql_result($result, $i, "a.travel_date")!="0000-00-00") { echo "<h4>Travel Date : ".$travelDateFormatted->format('Y-m-d')."</h4>"; } ?> 
           <?php if (mysql_result($result, $i, "a.price")!="0") { echo "<h4>Price per Seat-Trip: Rs.".nl2br(mysql_result($result,$i,"price")). "</h4>"; } ?> 
           <?php if (mysql_result($result, $i, "a.travel_distance")!="0") { echo "Distance (Km) : ".mysql_result($result,$i,"a.travel_distance")." / "; } if (mysql_result($result, $i, "a.travel_time")!="0") { echo "Duration (Hrs) : ".mysql_result($result,$i,"a.travel_time"); }  ?>
           
          
        </div>
        <div class="cus_row" style="width:12%; text-align: right;"></div>
      </div>
      <div>
        <div class="cus_row" style="width:10%; text-align: left;"> <img class="img-responsive" src="resources/icons/<?php echo showVehicleType(mysql_result($result,$i,"a.advert_id")); ?>_icon.png"  style="min-width: 40px;"/> </div>
        <div class="cus_row" style="width:68%;">
          
            <?php 
              if (mysql_result($result,$i,"split_cost")=="Y" && mysql_result($result,$i,"take_turns")=="Y") {
                echo "<p>Split Cost / Take Turns</p>"; 
              }elseif (mysql_result($result,$i,"split_cost")=="Y" && mysql_result($result,$i,"take_turns")=="N") {
                echo "<p>Split Cost</p>"; 
              }elseif (mysql_result($result,$i,"split_cost")=="N" && mysql_result($result,$i,"take_turns")=="Y") {
                echo "<p>Take Turns</p>";
              }  
           ?>
          
          <h4><?php if(mysql_result($result,$i,"mon")=="Y" or mysql_result($result,$i,"tue")=="Y" or mysql_result($result,$i,"wed")=="Y" or mysql_result($result,$i,"thu")=="Y" or mysql_result($result,$i,"fri")=="Y" or mysql_result($result,$i,"sat")=="Y" or mysql_result($result,$i,"sun")=="Y"){echo "Available Days:";} ?>
            <?php if(mysql_result($result,$i,"mon")=="Y"){echo "MON";} ?>   
            <?php if(mysql_result($result,$i,"tue")=="Y"){echo "TUE";} ?> 
            <?php if(mysql_result($result,$i,"wed")=="Y"){echo "WED";} ?> 
            <?php if(mysql_result($result,$i,"thu")=="Y"){echo "THU";} ?> 
            <?php if(mysql_result($result,$i,"fri")=="Y"){echo "FRI";} ?> 
            <?php if(mysql_result($result,$i,"sat")=="Y"){echo "SAT";} ?> 
            <?php if(mysql_result($result,$i,"sun")=="Y"){echo "SUN";} ?> 
          </h4>
          <?php 
              if (mysql_result($result,$i,"daily")=="Y") {
                echo "- Daily -"; 
              }
			  if (mysql_result($result,$i,"weekly")=="Y") {
                echo "- Weekly -"; 
              }
			  if (mysql_result($result,$i,"monthly")=="Y") {
                echo "- Monthly -";
              }  
           ?>
          <h4><?php echo "DEP :".mysql_result($result,$i,"origin_time") ?> - <?php echo  "RET :".mysql_result($result,$i,"dest_time") ?></h4>
        </div>
        <div class="cus_row" style="width:12%; text-align: right;"> <?php if (mysql_result($result,$i,"status")=="completed") {
                echo "<img class='img-responsive' src='resources/icons/completed.png' style='min-width: 40px;'/>"; 
              }  ?>  </div>
      </div>
    </div>
  </div>
 
 <?php
  }
  ?>
  
  <!-- Notification Panel -->
  <div id="includedNotifPanel"></div>
  
  <div style="height:44px"></div>
  <!-- Site footer -->
  <div class="navbar navbar-inverse navbar-fixed-bottom">
    <div class="navbar-inner"> <a class="brand" href="#" style="padding-left:7%">&copy; Carpooling.lk 2013</a> </div>
  </div>
</div>

<!-- JAVASCRIPT INCLUDES -->
<script type="text/javascript" src="resources/js/jquery.min.js"></script>
<script type="text/javascript" src="resources/js/bootstrap-modal.js"></script>
<script type="text/javascript" src="resources/js/bootstrap-transition.js"></script>
<script type="text/javascript" src="resources/js/bootstrap-collapse.js"></script>
<script type="text/javascript" src="resources/js/bootstrap-typeahead.js"></script>
<script type="text/javascript" src="resources/js/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="resources/js/jquery.timepicker.js"></script>
<script type="text/javascript" src="common/admin.js"></script>

<script type="text/javascript">
	//Include Notification panel
	$(function(){
      $("#includedNotifPanel").load("notification_panel.php"); 
    });

	//Include Search panel
	$(function(){
      $("#includedSrchPanel").load("search_panel.php"); 
    });
	
	$(".accept-btn").bind('click', function(){
      var id = $(this).attr('id');
	  var advert_type = $(this).attr('advType');
	  document.location.href = "view_advertiesement.php?advert_id=" + id + "&advert_type=" + advert_type;
    });

</script>
</body>
</html>